package com.demo.providerdemo;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.ContactsContract.RawContacts;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class ContactsActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, 100);
        setContentView(R.layout.contacts);
        // 获取系统界面中查找、添加两个按钮
        final Button search = (Button) findViewById(R.id.search);
        final Button add = (Button) findViewById(R.id.add);
        search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View source) {
                // 定义两个List来封装系统的联系人信息、指定联系人的电话号码、Email等详情
                final ArrayList<String> names = new ArrayList<String>();
                final ArrayList<ArrayList<String>> details = new ArrayList<ArrayList<String>>();
                // 使用ContentResolver查找联系人数据
                Cursor cursor = getContentResolver().query(
                        ContactsContract.Contacts.CONTENT_URI, null, null,
                        null, null);
                // 遍历查询结果，获取系统中所有联系人
                while (cursor.moveToNext()) {
                    // 获取联系人ID
                    String contactId = cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    // 获取联系人的名字
                    String name = cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    names.add(name);
                    // 使用ContentResolver查找联系人的电话号码
                    Cursor phones = getContentResolver().query(
                            Phone.CONTENT_URI,
                            null,
                            Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    ArrayList<String> detail = new ArrayList<String>();
                    // 遍历查询结果，获取该联系人的多个电话号码
                    while (phones.moveToNext()) {
                        // 获取查询结果中电话号码列中数据
                        String phoneNumber = phones.getString(phones
                                .getColumnIndex(Phone.NUMBER));
                        String phoneTpye = "电话号码:";
                        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
                        switch (type) {
                            case Phone.TYPE_HOME:
                                phoneTpye = "住宅号码:";
                                break;
                            case Phone.TYPE_MOBILE:
                                phoneTpye = "移动电话:";
                                break;
                            case Phone.TYPE_WORK:
                                phoneTpye = "公司电话:";
                                break;
                            default:
                                break;
                        }
                        detail.add(phoneTpye + phoneNumber);
                    }
                    phones.close();
                    // 使用ContentResolver查找联系人的Email地址
                    Cursor emails = getContentResolver().query(
                            Email.CONTENT_URI,
                            null,
                            Email.CONTACT_ID
                                    + " = " + contactId, null, null);
                    // 遍历查询结果，获取该联系人的多个Email地址
                    while (emails.moveToNext()) {
                        // 获取查询结果中Email地址列中数据。
                        String emailAddress = emails.getString(emails
                                .getColumnIndex(Email.DATA));
                        detail.add("邮件地址：" + emailAddress);
                    }
                    emails.close();
                    details.add(detail);
                }
                cursor.close();
                // 加载result.xml界面布局代表的视图
                View resultDialog = getLayoutInflater().inflate(
                        R.layout.result, null);
                // 获取resultDialog中ID为list的ExpandableListView
                ExpandableListView list = resultDialog.findViewById(R.id.list);
                // 创建一个ExpandableListAdapter对象
                ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

                    @Override
                    public int getGroupCount() {
                        return names.size();
                    }

                    // 该方法决定每个组选项的外观
                    @Override
                    public View getGroupView(int groupPosition,
                                             boolean isExpanded, View convertView,
                                             ViewGroup parent) {
                        LinearLayout layout = new LinearLayout(
                                ContactsActivity.this);
                        layout.setPadding(60, 10, 0, 10);
                        TextView textView = new TextView(ContactsActivity.this);
                        textView.setText(names.get(groupPosition));
                        textView.setTextSize(20);
                        layout.addView(textView);
                        return layout;
                    }

                    @Override
                    public int getChildrenCount(int groupPosition) {
                        return details.get(groupPosition).size();
                    }

                    // 该方法决定每个子选项的外观
                    @Override
                    public View getChildView(int groupPosition,
                                             int childPosition, boolean isLastChild,
                                             View convertView, ViewGroup parent) {
                        LinearLayout layout = new LinearLayout(
                                ContactsActivity.this);
                        layout.setPadding(30, 10, 0, 10);
                        TextView textView = new TextView(ContactsActivity.this);
                        textView.setText(details.get(groupPosition).get(
                                childPosition));
                        textView.setTextSize(20);
                        layout.addView(textView);
                        return layout;
                    }

                    // 获取指定组位置、指定子列表项处的子列表项数据
                    @Override
                    public Object getChild(int groupPosition, int childPosition) {
                        return details.get(groupPosition).get(childPosition);
                    }

                    @Override
                    public long getChildId(int groupPosition, int childPosition) {
                        return childPosition;
                    }

                    // 获取指定组位置处的组数据
                    @Override
                    public Object getGroup(int groupPosition) {
                        return names.get(groupPosition);
                    }

                    @Override
                    public long getGroupId(int groupPosition) {
                        return groupPosition;
                    }

                    @Override
                    public boolean isChildSelectable(int groupPosition,
                                                     int childPosition) {
                        return true;
                    }

                    @Override
                    public boolean hasStableIds() {
                        return true;
                    }
                };
                // 为ExpandableListView设置Adapter对象
                list.setAdapter(adapter);
                // 使用对话框来显示查询结果
                Builder b = new Builder(ContactsActivity.this);
                b.setView(resultDialog);
                b.setPositiveButton("确定", null);
                b.create().show();
            }
        });
        // 为add按钮的单击事件绑定监听器
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取程序界面中的3个文本框
                EditText edtName = (EditText) findViewById(R.id.name);
                EditText edtPhone = (EditText) findViewById(R.id.phone);
                EditText edtEmail = (EditText) findViewById(R.id.email);

                String name = edtName.getText().toString();
                String phone = edtPhone.getText().toString();
                String email = edtEmail.getText().toString();
                // 创建一个空的ContentValues
                ContentValues values = new ContentValues();
                // 向RawContacts.CONTENT_URI执行一个空值插入，
                // 目的是获取系统返回的rawContactId
                Uri rawContactUri = getContentResolver().insert(
                        RawContacts.CONTENT_URI, values);
                long rawContactId = ContentUris.parseId(rawContactUri);
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                // 设置内容类型
                values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
                // 设置联系人名字
                values.put(StructuredName.GIVEN_NAME, name);
                // 向联系人URI添加联系人名字
                getContentResolver().insert(
                        ContactsContract.Data.CONTENT_URI,
                        values);
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
                // 设置联系人的电话号码
                values.put(Phone.NUMBER, phone);
                // 设置电话类型
                values.put(Phone.TYPE, Phone.TYPE_MOBILE);
                // 向联系人电话号码URI添加电话号码
                getContentResolver().insert(
                        ContactsContract.Data.CONTENT_URI,
                        values);
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
                // 设置联系人的Email地址
                values.put(Email.DATA, email);
                // 设置该电子邮件的类型
                values.put(Email.TYPE, Email.TYPE_WORK);
                // 向联系人Email URI添加Email数据
                getContentResolver().insert(
                        ContactsContract.Data.CONTENT_URI,
                        values);
                edtName.setText("");
                edtEmail.setText("");
                edtPhone.setText("");
                Toast.makeText(ContactsActivity.this, "联系人数据添加成功", Toast.LENGTH_LONG).show();
            }
        });
    }
}